home *** CD-ROM | disk | FTP | other *** search
- Path: atglab.bls.com!Alun.Champion
- From: Alun.Champion@bridge.bst.bls.com (Alun Champion)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: 19 Jan 1996 19:39:04 GMT
- Organization: Computer People Inc.
- Message-ID: <ALUN.CHAMPION.96Jan19143904@g7240065.bridge.bst.bls.com>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com>
- <hamilton-1801962045570001@dialup-147.austin.io.com>
- NNTP-Posting-Host: bstfirewall.bst.bls.com
- In-reply-to: hamilton@shokwave.com's message of Thu, 18 Jan 1996 20:45:57
- -0600
-
- In article <hamilton-1801962045570001@dialup-147.austin.io.com> hamilton@shokwave.com (Jim Hamilton) writes:
-
- : In article <4dfhlu$a33$1@mhafn.production.compuserve.com>, Holger Maier
- : <100336.3326@CompuServe.COM> wrote:
-
- :> Consider
- :> #include <iostream>
- :> int main() {
- :> int i=1;int j=i+(i+=1);
- :> cout<<i<<','<<j<<'\n';
- :> return 0;
- :> }
-
- : The highest precedence in any expression is the insides of parentheses
- : (). Therefore (i+=1) is evaluated before i+().
-
- No
-
- i + (i += 1)
-
- Precedence alters the way the expression is parsed not the order in which
- it is evaluated
-
- With parenthisis Without parenthis
-
- op(+) op(+) ignoring side effect
- / \ / \ which would make this
- / \ / \ an illegal construct
- / \ / \
- expr(i) op(+=) op(+) expr(1)
- / \ / \
- / \ / \
- / \ / \
- expr(i) expr(1) expr(i) expr(i)
-
- if op is not a sequence point then it is implementation dependant on
- whether the left had side of the expression is evaluated before the
- right hand side of the expression. If op is a sequence point (like ',') then
- the left hand side is evaluated before the right.
-
- so if i = 1 going into the expression, it can be evaluated
-
- 1 + (i += 1)
- 1 + 2 /* i now equals 2 */
- 3
- or
- i + 2 /* i now equals 2 */
- 2 + 2
- 4
-
- Precedence does not dictate order.
-
- Regards
-
- -A.
- --
- | A.Champion |
-